// pages/search/index.js
const api = require('../../utils/request')
const utils = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scroll_height:0,
    keyword:'',
    history:[],
    list:[],
    currentPage:1,
    more: true,
    loading: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scroll_height: windowHeight * 750 / windowWidth - 78 - 10
    })
    let self = this
    wx.getStorage({
      key: 'search',
      success(res){
        console.log(res)
        if(res && res.data){
          self.setData({
            history: res.data
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindKeyInput(e){
    this.setData({
      keyword: e.detail.value
    })
  },
  onSearch(){
    if(this.data.keyword){
      if(!this.data.history.includes(this.data.keyword)){
        this.setData({
          history: [this.data.keyword,...this.data.history]
        },()=>{
          wx.setStorage({
            data: this.data.history,
            key: 'search',
          })
        })
      }
      this.search()
    }
  },
  onCancel(){
    if(this.data.keyword || this.data.list){
      this.setData({
        keyword: '',
        list: [],
        currentPage:1,
        loading: false,
        more: true
      })
    }
  },
  search(page){
    if(!page)page=this.data.currentPage;
    this.setData({
      loading: true
    })
    api.get(`/goods/search?page=${page}&limit=20&key=${this.data.keyword}`).then(res=>{
      let more = false
      if(res.pages > res.current){
        more = true
      }
      if(res.records.length>0){
        let goodsList = res.records
        for (let i = 0; i < goodsList.length; i++) {
          goodsList[i].price = utils.formatPrice(goodsList[i].price)
          goodsList[i].imgUrl = utils.fileMgrUrl + goodsList[i].pic
        }
        let list;
        if(page ===1 ){
          list = goodsList
        }else{
          list=[...this.data.list,...goodsList]
        }
        return this.setData({
          list,
          loading: false,
          more: more,
          currentPage: res.current
        })
      }else{
        return this.setData({
          loading: false
        })
      }
    })
  },
  loadMore() {
    if(this.data.more){
      wx.showLoading({
        title: '加载中',
      })
      this.search(this.currentPage+1).then(res=>{
        wx.hideLoading({
        })
      })
    }

  },
  addToCart(){
    wx.showToast({
      title: '加入购物车成功',
      icon: 'none',
      duration: 2000
    })
  },
  clearHistory(){
    if(this.data.history){
      this.setData({
        history: []
      })
      wx.removeStorage({
        key: 'search',
      })
      wx.showToast({
        title: '历史记录删除成功',
        icon: 'none'
      })
    }
  },
  textSelected(event){
    console.log(event)
    if(event.currentTarget.dataset.text){
      this.setData({
        keyword: event.currentTarget.dataset.text,
        loading: true,
        currentPage: 1
      },()=>{ this.search()})
      
    }
  }
})